package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketRole;
import com.cskaoyan.market.db.domain.MarketRoleExample;
import com.cskaoyan.market.db.mapper.MarketRoleMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminRoleServiceImpl implements AdminRoleService {

    @Override
    public List<MarketRole> list(String page, String limit, String sort, String order, String name) {
        SqlSession session = MybatisUtils.getSession();
        MarketRoleMapper marketRoleMapper = session.getMapper(MarketRoleMapper.class);
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        if (!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        marketRoleExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketRole> marketRole=marketRoleMapper.selectByExample(marketRoleExample);
        session.close();
        return marketRole;
    }

    @Override
    public void create(MarketRole marketRole) {
        SqlSession session = MybatisUtils.getSession();
        MarketRoleMapper marketRoleMapper = session.getMapper(MarketRoleMapper.class);
        marketRoleMapper.insertSelective(marketRole);
        session.commit();
        session.close();
    }

    @Override
    public void update(MarketRole marketRole) {
        SqlSession session = MybatisUtils.getSession();
        MarketRoleMapper marketRoleMapper = session.getMapper(MarketRoleMapper.class);
        marketRoleMapper.updateByPrimaryKey(marketRole);
        session.commit();
        session.close();
    }

    @Override
    public void delete(MarketRole marketRole) {
        SqlSession session = MybatisUtils.getSession();
        MarketRoleMapper marketRoleMapper = session.getMapper(MarketRoleMapper.class);
        marketRoleMapper.deleteByPrimaryKey(marketRole.getId());
        session.commit();
        session.close();
    }
}
